package cn.xinfei.xdecision.manager.controller.component;

import cn.xinfei.xdecision.common.model.common.PageResult;
import cn.xinfei.xdecision.common.model.common.PageResultNew;
import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.common.requestParam.AssemblyRuleCodeReq;
import cn.xinfei.xdecision.common.model.component.ComponentVo;
import cn.xinfei.xdecision.common.model.component.dependency.EngineNodeComponentRel;
import cn.xinfei.xdecision.common.model.component.dependency.resp.DecisionComponentDependencyResp;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleListReqVo;
import cn.xinfei.xdecision.common.service.component.ComponentCommonContext;
import cn.xinfei.xdecision.common.service.component.ComponentCommonService;
import cn.xinfei.xdecision.common.service.component.dependency.EngineNodeComponentRelService;
import cn.xinfei.xdecision.common.service.utils.CodeGenerateUtil;
import cn.xinfei.xdecision.common.utils.enums.enginex.ActionEnum;
import cn.xinfei.xdecision.common.utils.enums.enginex.action.ActionPostTypeEnum;
import cn.xinfei.xdecision.common.utils.enums.enginex.action.ActionServerEnum;
import cn.xinfei.xdecision.common.utils.exception.ApiException;
import cn.xinfei.xdecision.common.utils.exception.enums.ErrorCodeEnum;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static cn.xinfei.xdecision.common.utils.exception.enums.ErrorCodeEnum.PARAMS_EXCEPTION;

/**
 * 组件中心
 */
@RestController
@RequestMapping("/v3/component/")
public class ComponentCommonController {

    @Resource
    private EngineNodeComponentRelService engineNodeComponentRelService;

    @Resource
    private CodeGenerateUtil codeGenerateUtil;

    @Resource
    private ComponentCommonContext componentCommonContext;

    @ApiOperation(value = "生成规则code")
    @PostMapping(value = "codeGenerate/{code}")
    public ResponseEntityDto<String> codeGenerate(@PathVariable String code) {
        return ResponseEntityBuilder.buildNormalResponse(codeGenerateUtil.generateYMDCode(code, null));
    }

    @ApiOperation(value = "是否有新建版本")
    @GetMapping(value = "hasNewVersion")
    public ResponseEntityDto<Boolean> hasNewVersion(@RequestParam(value = "type") String type,
                                                    @RequestParam(value = "ruleCode") String ruleCode) {
        ComponentCommonService targetComponent = ComponentCommonService.getTargetComponentByType(type);
        if (Objects.isNull(targetComponent)) {
            return ResponseEntityBuilder.buildErrorResponse(ErrorCodeEnum.COMPONENT_TYPE_ERROR);
        }
        return ResponseEntityDto.success(targetComponent.hasNewVersion(ruleCode));
    }


    @ApiOperation(value = "查看依赖关系")
    @PostMapping(value = "getDependencyByComponentCode")
    public ResponseEntityDto<PageResultNew<DecisionComponentDependencyResp>> getDependencyByComponentCode(@RequestBody AssemblyRuleCodeReq assemblyRuleCodeReq) {
        if (CollectionUtils.isEmpty(assemblyRuleCodeReq.getRuleCodes())) {
            throw new ApiException(PARAMS_EXCEPTION);
        }
        return engineNodeComponentRelService.getRelByComponentCode(assemblyRuleCodeReq);
    }

    @ApiOperation(value = "批量查询规则详情和版本信息")
    @GetMapping(value = "listComponentInfoByIds")
    public ResponseEntityDto<List<ComponentVo>> listComponentInfoByIds(@RequestParam(value = "ids") String ids,
                                                                       @RequestParam(value = "type") String type) {
        ComponentCommonService targetComponent = ComponentCommonService.getTargetComponentByNodeType(type);
        if (Objects.isNull(targetComponent)) {
            return ResponseEntityBuilder.buildErrorResponse(ErrorCodeEnum.COMPONENT_TYPE_ERROR);
        }
        return ResponseEntityDto.success(targetComponent.listComponentInfoByIds(ids));
    }

    @ApiOperation(value = "通过变量code集合查询依赖关系")
    @PostMapping(value = "getDependencyByVarCode")
    public ResponseEntityDto<PageResult<List<DecisionComponentDependencyResp>, EngineNodeComponentRel>> getDependencyByVarCode(@RequestBody AssemblyRuleCodeReq assemblyRuleCodeReq) {
        return engineNodeComponentRelService.getRelByVarCode(assemblyRuleCodeReq);
    }

    @ApiOperation(value = "按条件查询决策流内组件中心的列表")
    @PostMapping(value = "pageComponentInfoByCon")
    public ResponseEntityDto<PageResultNew<ComponentVo>> pageComponentInfoByCon(@RequestBody RuleListReqVo ruleListReqVo) {
        return componentCommonContext.pageComponentInfoByCon(ruleListReqVo);
    }

    @ApiOperation(value = "枚举接口")
    @GetMapping(value = "getEnum")
    public ResponseEntityDto<Map<String, String>> getEnum() {
        Map<String, String> map = new HashMap<>();
        map.put("actionEnum", JSON.toJSONString(ActionEnum.values()).toLowerCase(Locale.ROOT));
        map.put("actionPostTypeEnum", ActionPostTypeEnum.convertEnum());
        map.put("actionServerEnum", ActionServerEnum.convertEnum());
        return ResponseEntityDto.success(map);
    }
}
